//
//  SoundManager.mm
//
//  Created by Enric Vergara Carreras on 01/01/11.
//  Copyright 2011 Owlab. All rights reserved.
//

#import "SoundManager.h"
#import "SimpleAudioEngine.h"

static SoundManager *sharedInstanceSM = nil;

@implementation SoundManager

#pragma mark -
#pragma mark class instance methods

- ( id ) init 
{	
    [self initSoundManager];
	return self;
}

- (void) initSoundManager
{
 
    m_fSoundFXVolume = 1.f;
    [self setSoundFXVolume:m_fSoundFXVolume];
    
    m_Sounds = [[NSDictionary alloc]  initWithObjectsAndKeys: 
                @"gui_back.wav", [NSNumber numberWithInt:SOUND_BUTTON_NEXT],
                @"gui_next.wav", [NSNumber numberWithInt:SOUND_BUTTON_BACK],
                @"Button_9.wav", [NSNumber numberWithInt:SOUND_BUTTON_CANCEL],
                @"gameOver.wav", [NSNumber numberWithInt:SOUND_GAME_OVER],
                @"youwin.wav",   [NSNumber numberWithInt:SOUND_YOU_WIN],
                @"help.wav",     [NSNumber numberWithInt:SOUND_SECOND_MENU_LOOP],
                @"Button_8.wav", [NSNumber numberWithInt:SOUND_BUTTON_SHOW_CARDS],
                @"loop-gen.wav", [NSNumber numberWithInt:SOUND_MAIN_MENU_LOOP], nil ];
    
    for (id key in m_Sounds)
    {
        NSLog(@"SoundSystem key: %@   value:%@", key, [m_Sounds objectForKey:key]);
        [[SimpleAudioEngine sharedEngine] preloadEffect:[m_Sounds objectForKey:key]];
    }
}



- (void) playEffect:(TypeSound) internal_id
{

    NSNumber *itemId = [NSNumber numberWithInt:internal_id];
    NSString* value = [m_Sounds objectForKey:itemId];
    
    if (value != nil)
    {
        [[SimpleAudioEngine sharedEngine] playEffect:value];
    }
}

- (void) playMusic:(TypeSound) internal_id loop:(bool) l
{
    
    NSNumber *itemId = [NSNumber numberWithInt:internal_id];
    NSString* value = [m_Sounds objectForKey:itemId];
    
    if (value != nil)
    {
        [[SimpleAudioEngine sharedEngine] playBackgroundMusic:value loop:l];
    }
}

- (void) stopMusic
{
    [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
}

- (void) setSoundFXVolume:(float) volume
{
    m_fSoundFXVolume = volume;
    if (m_fSoundFXVolume > 1.f) m_fSoundFXVolume = 1.f;
    if (m_fSoundFXVolume < 0.f) m_fSoundFXVolume = 0.f;
   
    [SimpleAudioEngine sharedEngine].effectsVolume = m_fSoundFXVolume;
    [SimpleAudioEngine sharedEngine].backgroundMusicVolume = m_fSoundFXVolume;
}

-(float) getGeneralSoundFXVolume
{
    return m_fSoundFXVolume;
}


//---------------------------------------------------------
//---------------------------------------------------------
#pragma mark -
#pragma mark Singleton methods


+ (SoundManager*)sharedInstance
{
    @synchronized(self)
    {
        if (sharedInstanceSM == nil)
			sharedInstanceSM = [[SoundManager alloc] init];
    }
    return sharedInstanceSM;
}

+ (id)allocWithZone:(NSZone *)zone 
{
    @synchronized(self) 
    {
        if (sharedInstanceSM == nil) 
        {
            sharedInstanceSM = [super allocWithZone:zone];
            return sharedInstanceSM;  // assignment and return on first allocation
        }
    }
    return nil; // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain 
{
    return self;
}

- (unsigned)retainCount 
{
    return UINT_MAX;  // denotes an object that cannot be released
}



- (id)autorelease 
{
	[self release];
    return self;
}

- (void)release 
{
    [m_Sounds release];
}


@end